﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Utility;
using Utility.Json;
using MsgCenter.Web.Handlers;

namespace MsgCenter.Web
{
    /// <summary>
    /// WS_Message1 的摘要说明
    /// </summary>
    public class WS_Message1 : IHttpHandler
    {
        /// <summary>
        /// 获取与该页关联的 HttpContext 对象。
        /// </summary>
        public HttpContext Context { get; private set; }
        /// <summary>
        /// 获取请求的页的 HttpRequest 对象。
        /// </summary>
        public HttpRequest Request { get; private set; }
        /// <summary>
        /// 获取与该 Page 对象关联的 HttpResponse 对象。该对象使您得以将 HTTP 响应数据发送到客户端，并包含有关该响应的信息。
        /// </summary>
        public HttpResponse Response { get; private set; }


        private string RequestValue(string sParam)
        {
            return (Request.Params[sParam] == null ? string.Empty : Request.Params[sParam].ToSafeString().ToUrlDecode());
        }

        #region 调用客户端IP地址
        /// <summary>
        /// 调用客户端IP地址
        /// </summary>
        /// <returns></returns>
        private string GetIP()
        {
            string _ip = string.Empty;
            try
            {
                if (Context.Request.ServerVariables["HTTP_VIA"] != null) // using proxy
                {
                    _ip = Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();  // Return real client IP.
                }
                else// not using proxy or can't get the Client IP
                {
                    _ip = Context.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP.
                }
            }
            catch
            { }
            return _ip;
        }
        #endregion

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            this.Context = context;
            this.Request = context.Request;
            this.Response = context.Response;
            string strMethod = RequestValue("method").ToSafeString();
            string strResponseMessage = "";
            try
            {
                if (strMethod == "WS_SendMessge")
                {
                    string strMessage = RequestValue("Message").ToSafeString();
                    string strMessageID = RequestValue("MessageID").ToSafeString();
                    if (strMessageID.IsNullOrEmpty())
                    {
                        strMessageID = "接口(ashx)发送" + DateTime.Now.DateTimeToyyyyMMddHHmmss();
                    }
                    string strApplicationCode = RequestValue("ApplicationCode").ToSafeString();
                    string strTempletNo = RequestValue("TempletNo").ToSafeString();
                    string strReceiveLoginName = RequestValue("ReceiveLoginName").ToSafeString();
                    JsonRetData json = WS_Send.WS_SendMessge(strMessageID, strApplicationCode, strTempletNo, strMessage.Split('|'), strReceiveLoginName, GetIP());
                    if (json.State == "true")
                    {
                        strResponseMessage = "1";
                    }
                    else
                    {
                        strResponseMessage = "0";
                    }
                }
                else if (strMethod == "WS_SendMessge_Mobile")
                {
                    string strMessage = RequestValue("Message").ToSafeString();
                    string strMessageID = RequestValue("MessageID").ToSafeString();
                    string strApplicationCode = RequestValue("ApplicationCode").ToSafeString();
                    string strTempletNo = RequestValue("TempletNo").ToSafeString();
                    string strMobile = RequestValue("Mobile").ToSafeString();
                    if (strMessageID.IsNullOrEmpty())
                    {
                        strMessageID = "接口(ashx)发送" + DateTime.Now.DateTimeToyyyyMMddHHmmss();
                    }
                    JsonRetData json = WS_Send.WS_SendMessge_Mobile(strMessageID, strApplicationCode, strTempletNo, strMessage.Split('|'), GetIP(), strMobile);
                    if (json.State == "true")
                    {
                        strResponseMessage = "1";
                    }
                    else
                    {
                        strResponseMessage = "0";
                    }
                }
                else if (strMethod == "WS_SendMessge_Role")
                {
                    string strMessage = RequestValue("Message").ToSafeString();
                    string strMessageID = RequestValue("MessageID").ToSafeString();
                    string strApplicationCode = RequestValue("ApplicationCode").ToSafeString();
                    string strTempletNo = RequestValue("TempletNo").ToSafeString();
                    string strMobile = RequestValue("Mobile").ToSafeString();
                    if (strMessageID.IsNullOrEmpty())
                    {
                        strMessageID = "接口(ashx)发送" + DateTime.Now.DateTimeToyyyyMMddHHmmss();
                    }
                    JsonRetData json = WS_Send.WS_SendMessge_Role(strMessageID, strApplicationCode, strTempletNo, strMessage.Split('|'), GetIP(), strMobile);
                    if (json.State == "true")
                    {
                        strResponseMessage = "1";
                    }
                    else
                    {
                        strResponseMessage = "0";
                    }
                }
                else
                {
                    strResponseMessage = "0";
                    //JsonRetData json = new JsonRetData();
                    //json.State = "false";
                    //json.Message = "发送失败：没有找到对应的发送方法";
                    //strResponseMessage = json.AssemblyJson();
                }
            }
            catch (Exception ex)
            {
                strResponseMessage = "0";
                //JsonRetData json = new JsonRetData();
                //json.State = "false";
                //json.Message = "发送失败：" + ex.Message;
                //strResponseMessage = json.AssemblyJson();
            }
            this.Response.Write(strResponseMessage);
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}